var ak = require('/Applications/Audiokinetic/Wwise 2017.2.6.6636/SDK/include/AK/WwiseAuthoringAPI/js/waapi.js').ak;
var autobahn = require('autobahn');
var wwisedata = require('./wwisedata');
var bank = require('./soundbank');

var soundbanks = [];

// Create the WAMP connection
var connection = new autobahn.Connection({
        url: 'ws://localhost:8080/waapi',
        realm: 'realm1',
        protocols: ['wamp.2.json']
    });

// Setup handler for connection opened
connection.onopen = function (session) {

    // Call getInfo
    var query = {
        from:
            {path: ['\\SoundBanks']},
        transform:[
            {select:['descendants']},
            //{where:['name:matches','^My']}
        ]
    };
    
    var options = {
        return: ['id', 'name', 'type', 'parent', 'soundbank:bnkFilePath']
    };
    
    session.call('ak.wwise.core.object.get', [], query, options).then(
        function (res) {
            // Print the name
            var objects = res.kwargs.return;
            for(let i = 0; i < objects.length;++i ){
                if (objects[i].type == 'SoundBank'){
                    console.log(`${objects[i].name}` + ` ${objects[i].type}` + ` ${objects[i].parent.name}` + ` ${objects[i]['soundbank:bnkFilePath']}`);
                    soundbank = new bank(objects[i].id, objects[i].name, objects[i]['soundbank:bnkFilePath']);
                    soundbanks.push(soundbank);
                }
            }
        },
        function (error) {
            console.log(`error: ${error}`);
        }
    ).then(
        function(){
            connection.close();
        }
    );
};

connection.onclose = function (reason, details) {
    if (reason !== 'lost') {
        console.log("Connection closed. Reason: " + reason);
    }
    process.exit();
};

// Open the connection
connection.open();

